//
//  QSRefreshTestPage.swift
//  chat
//
//  Created by 1 on 2024/2/22.
//  使用参考QSRefreshTestPage

import SwiftUI

struct User: Identifiable {
    var id = UUID().uuidString
}

class ViewModel: ObservableObject {
    @Published var state: RefreshableListState = .initialize
    @Published var users: [User] = []

    func initUsers() {
       DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
           self.users.removeAll()
           for _ in (0 ..< 20) {
               self.users.append(User())
           }
           self.state = .displayList
       }
    }

    func appendUsers() {
        state = .append
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
            for _ in (0 ..< 10) {
                self.users.append(User())
            }
            self.state = .displayList
        }
    }
}

struct QSRefreshTestPage: View {
    @StateObject private var viewModel: ViewModel = .init()

    var body: some View {
        NavigationView {
            QSRefreshableList(state: $viewModel.state) {
                ForEach(viewModel.users, id: \.id) { user in
                    Text(user.id.prefix(13))
                        .onAppear {
                            // 追加
                            if viewModel.users.last?.id == user.id {
                                viewModel.appendUsers()
                            }
                        }
                }
            } onRefersh: {
                viewModel.initUsers()
            }
        }
        .onAppear {
            viewModel.initUsers()
        }
    }
}

#Preview {
    QSRefreshTestPage()
}
